<template>
    <div id="seachDialog">
      <a-modal
        title="注册"
        v-model="caseData.show"
        width="1000px"
        style="overflow:hidden"
        @cancel="handleClose">
        <el-row style="padding: 30px 40px 30px 50px">
          <el-col :span="15">
            <el-form size="small" :model="parames" style="margin-top:20px auto" :rules="rules" label-position="left" ref="parames" label-width="140px" class="zkRegister">
              <el-form-item label="主营行业:" prop="industry">
                  <el-cascader
                    @change="changeIndustry"
                    style="width:100%"
                    v-model="parames.industry"
                    :options="subjectData">
                  </el-cascader>
                </el-form-item>
                <el-form-item>
                  <p v-if="parames2.smallScaleTaxRate">小规模纳税人税率：{{parames2.smallScaleTaxRate}}</p>
                  <p v-if="parames2.generalTexRate">一般纳税人税率：{{parames2.generalTexRate}}</p>
                  <p v-if="parames2.industryInformation">行业资讯：{{parames2.industryInformation}}</p>
                </el-form-item>
                <el-form-item label="经营范围:">
                  <el-select style="width:100%" size="small" v-model="parames.businessScope" @change="changeBusinessScope">
                    <el-option v-for="(item,index) in businessScopeData" :key="index" :label="item.label" :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="行业许可证:">
                  <el-input disabled v-model="parames.license" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="企业类型:" prop="companyTypeId">
                  <el-select style="width:100%" size="small" v-model="parames.companyTypeId">
                    <el-option v-for="(item,index) in companyData" :key="index" :label="item.typeName" :value="item.id"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="注册资金:">
                  <el-input type="number" placeholder="请输入" v-model="parames.turnover">
                    <template slot="append">万元</template>
                  </el-input>
                </el-form-item>
                <el-form-item label="注册区域:" prop="administrativeRegion">
                  <el-cascader
                  placeholder="请选择"
                  style="width:100%"
                  :options="options"
                  v-model="parames.administrativeRegion"
                  >
                  </el-cascader>
                </el-form-item>
              </el-form>
          </el-col>
          <el-col :span="8">
            <p style="margin-left:40px">成功案例:</p>
              <div v-for="(item,index) in parames2.successCaseList" :key="index" style="background: #fff;padding: 10px;margin: 10px 0 0 40px;border: #dbdbdb solid 1px">
                <p v-if="item.customerName">客户名称：{{item.customerName}}</p>
                <p v-if="item.industryName">主营行业：{{item.industryName}}</p>
                <p v-if="item.area">注册地：{{item.area}}</p>
                <p v-if="item.registeredCapital">注册资金：{{item.registeredCapital}}(万元)</p>
              </div>
          </el-col>
        </el-row>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" type="primary" @click="handleSend">保存</el-button>
            <el-button size="small" @click="handleClose">取消</el-button>
        </span>
      </a-modal>
    </div>
</template>

<script>
import { provinceAndCityData, regionData, provinceAndCityDataPlus, regionDataPlus, CodeToText, TextToCode } from 'element-china-area-data'
export default {
    props: ['caseData'],
    data() {
        return {
          options: regionData,
          parames: {
            industry: [],
            industryId: '', //主营行业ID
            businessScope: '', //经营范围
            license: '', //行业许可证
            companyTypeId: '', //企业类型
            administrativeRegion: [], //注册行政区域
            turnover: '', //营业额
          },
          parames2: {
            smallScaleTaxRate: '',
            generalTexRate: '',
            industryInformation: '',
            businessScopeList: [],
            successCaseList: []
          },
          subjectData: [],
          businessScopeData: [],
          companyData: [],
          rules: {
            industry: [
              { required: true, message: '必填', trigger: ['blur','change'] },
            ],
            businessScope: [
              { required: true, message: '必填', trigger: ['blur','change']},
            ],
            companyTypeId: [
              { required: true, message: '必填', trigger: ['blur','change'] },
            ],
            administrativeRegion: [
              { required: true, message: '必填', trigger: ['blur','change'] },
            ],
          }
        }
    },

    methods: {
      changeBusinessScope () { // 选择范围
        this.parames2.businessScopeList.forEach(el=>{
          if (this.parames.businessScope == el.id) {
            this.parames.license = el.industryQualificationLicense
          }
        })
      },
      changeIndustry (key) { // 选择行业
        this.$post(this.$api.postCustomerBaseInfoIndustryFind,{
          industryId: this.parames.industry[1]
        }).then(response => {
          if (response.data.succeed) {
            let data = response.data.data
            this.parames2.smallScaleTaxRate = data.smallScaleTaxRate//小规模纳税人税率
            this.parames2.generalTexRate = data.generalTexRate//一般纳税人税率
            this.parames2.industryInformation = data.industryInformation//行业资讯,
            this.parames2.businessScopeList = data.businessScopeList
            this.businessScopeData = []
            data.successCaseList.forEach(el=>{
              if (el.administrativeRegion) {
                let area = el.administrativeRegion
                el.area = CodeToText[Number(area[0])] + '-' + CodeToText[Number(area[1])] + '-' + CodeToText[Number(area[2])]
              }
            })
            this.parames2.successCaseList = data.successCaseList
            data.businessScopeList.forEach(el=>{
              this.businessScopeData.push({
                label: el.businessScopeName,
                value: el.id,
                license: el.industryQualificationLicense
              })
            })
            if (key !== 'render') {
              this.parames.businessScope = ''
              this.parames.license = ''
            }
          }
        })
      },
      handleClose () {
        this.caseData.show = false
      },
      addressChange () {},
      getCompanyType () { //获取企业类型
        this.$post(this.$api.postCustomerBaseInfoInCompanyTypeList,this.parames).then(response => {
            if (response.data.succeed) {
                this.companyData = response.data.data
            }
        })
      },
      getTree () { //获取树组织
        this.$post(this.$api.postCustomerBaseInfoIndustryTree).then(response => {
          if (response.data.succeed) {
            this.subjectData = []
            if (response.data.data.length > 0) {
              response.data.data.forEach(el=>{
                this.getData(el)
                if (el.label) {
                  this.subjectData.push(el)
                }
              })
            }
          }
        })
      },
      getData(data) { //处理树数据
        if (data.industryName) {
          data.label = data.industryName
        }
        data.value = data.id
        if (data.childList.length>0) {
          data.children = data.childList
        }
        if (data.children && data.children.length > 0) {
          for(var i = 0; i < data.children.length; i++) {
            this.getData(data.children[i])
          }
        }
      },
      handleSend () { //保存
        this.$refs['parames'].validate((valid) => {
          if (valid) {
            this.parames.industryId = this.parames.industry[0]
            this.parames.childIndustryId = this.parames.industry[1]
            if (this.caseData.type == 'new') {
              this.$post(this.$api.postThinkTankAdd,this.parames).then(response => {
                if (response.data.succeed) {
                  this.$emit('getThink',response.data.data.thinkTankId)
                  this.handleClose()
                }
              })
            } else if (this.caseData.type == 'edit') {
              this.$post(this.$api.postThinkTankEdit,this.parames).then(response => {
                if (response.data.succeed) {
                  this.handleClose()
                }
              })
            }
            
          }
        })
      },
      render () {
        this.$post(this.$api.postThinkTankFind,{
          thinkTankId: this.caseData.id
        }).then(response => {
          if (response.data.succeed) {
            let data = response.data.data
            this.parames.id = data.id
            this.parames.industry = [data.industryId,data.childIndustryId]
            this.parames.industryId = data.industryId
            this.parames.childIndustryId = data.childIndustryId
            this.parames.companyTypeId = data.companyTypeId
            this.parames.administrativeRegion = data.administrativeRegion
            this.parames.turnover = data.turnover
            this.changeIndustry('render')
            this.parames.businessScope = data.businessScope
            this.parames.license = data.license
          }
        })
      },
      resetData () {
        this.parames.id = ''
        this.parames.industry = []
        this.parames.industryId = '' //主营行业ID
        this.parames.businessScope = '' //经营范围
        this.parames.childIndustryId = ''
        this.parames.license = '' //行业许可证
        this.parames.companyTypeId = '' //企业类型
        this.parames.administrativeRegion = [] //注册行政区域
        this.parames.turnover = '' //营业额
        this.parames2.smallScaleTaxRate = ''
        this.parames2.generalTexRate = ''
        this.parames2.industryInformation = ''
        this.parames2.businessScopeList = []
      }
    },
    computed: {
      changeDialog () {
        return this.caseData.show
      }
    }, 
    watch: {
      changeDialog: {
        handler (curVal, oldVal) {
          if (curVal) {
            this.getTree()
            this.getCompanyType()
            if (this.caseData.type == 'edit') {
              this.render()
            }
          } else {
            this.resetData()
          }
        }
      }
    },
};
</script>
